import pandas as pd
import numpy as np
import pymysql


class Index_charts:
	def __init__(self, df):
		# self.conn = pymysql.Connection(host="localhost", port=3306, user="root", password="123456", database="test1")
		# self.conn = pymysql.Connection(host="localhost", port=3306, user="root", password="123456", database="lianjia")
		# pd.read_sql_query("select d.rent,d.squares,c.district,c.town,c.province,d.label from city c,details d where d.citycode=c.citycode",self.conn)
		self.df = df
		self.df["size"] = self.df["squares"].str.extract(".*?(\d+)㎡").astype('int32')  # 使用正则匹配，并且将匹配结果转为int32类型数据
		self.df["toilet"] = self.df["squares"].str.extract(".*?(\d+)卫")
		self.df["hall"] = self.df["squares"].str.extract(".*?(\d+)室")
		self.df["room"] = self.df["squares"].str.extract(".*?(\d+)厅")
		df = self.df.dropna(axis=0, how="any")
		df["toilet"] = df["toilet"].astype('int32')
		df["hall"] = df["hall"].astype('int32')
		df["room"] = df["room"].astype('int32')
		self.data = df
		self.df.loc[:, "squares"].value_counts()
		self.data["kj"] = self.data["squares"].str.extract("(.*?)\d+㎡")

	def work(self):

		# n = self.datadata["squares"].groupby(self.data["town"]).value_counts()
		u = list(self.data.loc[:, "kj"].value_counts().keys())
		cis = list(self.data.loc[:, "kj"].value_counts())
		fx = []
		sl = []
		for i in range(len(u)):
			if cis[i] >= 10:
				fx.append(u[i])
				sl.append(cis[i])

		return fx, sl

	def map(self):
		k = self.df.groupby(["province"], as_index=False).mean()
		nam = list(k["province"])
		mon = list(round(k["rent"], 2))
		# print(nam, mon)
		return nam, mon

	def pie(self):
		m = self.df.groupby(["town"], as_index=False).mean()
		name = list(m["town"])
		money = list(round(m["rent"], 2))
		return name, money

	def bar(self):
		# print(self.data)
		o = self.data.groupby(["kj"], as_index=False).mean()
		money = list(round(o["rent"], 2))
		title = list(o["kj"])
		siz = list(round(o["size"]))
		return title, money, siz

	# print("o",o, "p:",p,"v:",v)
	def rigth(self):
		data = self.df
		data = pd.Series(data['town']).value_counts()
		city_data = list(data.index)
		data = list(data.values)
		data = [int(i) for i in data]
		# print(data)
		# print(type(data))
		# print(data)
		# print(city_data,data)
		return city_data, data

	def shuliang(self):
		# print(self.df)
		count = len(list(self.df.index))
		a = 0
		for i in self.df["squares"]:
			if "精装修" in i:
				a += 1
			# print(i)
		# (round((count-a)/count, 2) * 100)
		return float(round((count - a) / count, 2) * 100), a

	def left_work(self):
		a = self.df["label"].values
		b = ''.join(a)
		c = b.split(',')
		data = pd.Series(c).value_counts()
		name = data.index
		na = []
		dad = data.values
		da = []
		for i in range(len(name)):
			if dad[i] > 50:
				na.append(name[i])
				da.append(int(dad[i]))
		return na, da
if __name__ == '__main__':
	Index_charts().left_work()
	# print(a,b,c)
	# a, b = Index_charts().work()
	# print("a:", a, "b:", b)
